package dhm.com.dhmshop.updata.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.order.ChildOrderAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetAllOrder;
import dhm.com.dhmshop.entity.WacherBean;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.view.home.OrderdetailActivity;

public class TopaydetailActivity extends BaseActiity implements LoginContract.IView, IWXAPIEventHandler {


    @BindView(R.id.nameshop)
    TextView nameshop;
    @BindView(R.id.youfei)
    TextView youfei;
    @BindView(R.id.allmoney)
    TextView allmoney;
    @BindView(R.id.recy)
    RecyclerView recy;
    private String uid;

    private PressenterImpl pressenter;
    private GetAllOrder.DataBean orderbean;

    @Override
    protected int getLayout() {
        return R.layout.activity_topaydetail;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        orderbean = (GetAllOrder.DataBean) getIntent().getSerializableExtra("orderbean");


        uid = SpUtils.getString(this, "uid");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        ChildOrderAdapter childOrderAdapter = new ChildOrderAdapter(this);

        recy.setLayoutManager(manager);
        recy.setAdapter(childOrderAdapter);
        childOrderAdapter.setImageList(orderbean.getList());

        nameshop.setText(orderbean.getOrderNo());
        youfei.setText(orderbean.getDeliverMoney());
        allmoney.setText(orderbean.getRealTotalMoney());


    }

    @Override
    protected void initData() {
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof WacherBean) {
            WacherBean orderInfo = (WacherBean) data;
            wxPay(orderInfo);
        }
    }

    @Override
    public void fail(String error) {

    }


    private void wxPay(WacherBean wacherBean) {
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.Wx_APP_ID);
        api.handleIntent(getIntent(), this);
        api.registerApp(Constant.Wx_APP_ID);
        PayReq req = new PayReq();
//        orderInfo=orderInfo.replaceAll(" ","");
//        String[] split = orderInfo.split("\\{");
//        String splits="{"+split[1];
//        Gson gson=new Gson();
//        wacherBean = gson.fromJson(splits, WacherBean.class);

        req.appId = wacherBean.getAppid();
        req.partnerId = wacherBean.getPartnerid();
        req.prepayId = wacherBean.getPrepayid();
        req.packageValue = wacherBean.getPackageX();
        req.nonceStr = wacherBean.getNoncestr();
        req.timeStamp = wacherBean.getTimestamp();
        req.sign = wacherBean.getSign();
        api.sendReq(req);

    }



    @OnClick({R.id.back, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sure:
                Map<String, String> map = new HashMap<>();
                map.put("uid", uid);
                map.put("order_id", orderbean.getOrderId());
                pressenter.sendMessage(this, Constant.toOrderPay, map, WacherBean.class);
                break;
                default:
        }
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            switch (resp.errCode) {
                case 0://支付成功
                    Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
                    break;
                case -1://错误，可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
                    Toast.makeText(this, "支付错误" + resp.errCode, Toast.LENGTH_SHORT).show();
                    break;
                case -2://用户取消，无需处理。发生场景：用户不支付了，点击取消，返回APP。
                    Toast.makeText(this, "用户取消" + resp.errCode, Toast.LENGTH_SHORT).show();
                    break;
                    default:

            }

            finish();//这里需要关闭该页面
        }
    }
}
